package com.tommy.ddp.road
{
	import com.tommy.ddp.GridDataManager;
	import com.tommy.ddp.definition.DDPConfig;
	import com.tommy.ddp.definition.GridSkinFactory;
	import com.tommy.ddp.grid.DdpGrid;
	
	import flash.geom.Point;
	import flash.globalization.Collator;

	/**
	 * 
	 * @author hongxuan
	 * 
	 */	
	public class RoadFinder
	{
		//private static var map:Array;
		
		private var _path:Vector.<Point>;
		private var L_OR_R:uint;
		
		public function RoadFinder()
		{
		}
		
		
		public function find(column:int,row:int):void
		{
			_path = new Vector.<Point>();
			_path.push(new Point(column,row));
			
			var grid:DdpGrid = GridDataManager.getGrid(column,row);
			_readlFind(column,row);
			
			
			var p:Point = _path[_path.length-1];
			grid = GridDataManager.getGrid(p.x,p.y);
			if(grid)
				grid.isOccupied = true;
		}
		
		public function get path():Vector.<Point>
		{
			return _path;
		}
		
		private function _readlFind(column:int,row:int):void
		{
			var down_grid:DdpGrid = GridDataManager.getGrid(column,row+1);
			
			if(row < 0)
			{
				if(row == -1)
				{
					if(down_grid && down_grid.isCanFind)
					{
						_path.push(new Point(column,row+1));
						_readlFind(column,row+1);
					}
				}
				else
				{
					_path.push(new Point(column,row+1));
					_readlFind(column,row+1);
				}
				return;
			}
			var currentGrid:DdpGrid = GridDataManager.getGrid(column,row);
			if(!currentGrid.isAvaliable)
				return;
			
			if(!down_grid)
			{
				if(currentGrid.connectGrid)
				{
					var p:Point = new Point(currentGrid.connectGrid.column,currentGrid.connectGrid.row);
					down_grid = GridDataManager.getGrid(p.x,p.y);
				}
					
			}
			
			//
			var left_grid:DdpGrid = GridDataManager.getGrid(column-1,row);
			var right_grid:DdpGrid = GridDataManager.getGrid(column+1,row);
			
			var l_r_arr:Array = [left_grid,right_grid];
			
			if(down_grid && down_grid.isCanFind)
			{
				_path.push(new Point(down_grid.column,down_grid.row));
				_readlFind(down_grid.column,down_grid.row);
			}
			else
			{
				L_OR_R = Math.floor(Math.random()*2);
				var LorR_grid:DdpGrid = l_r_arr[L_OR_R];
				l_r_arr.splice(L_OR_R,1);
				
				var oblique_grid:DdpGrid;
				
				if(!LorR_grid || (LorR_grid.isOccupied && LorR_grid.isAvaliable) || LorR_grid.isAvaliable)
				{
					LorR_grid = l_r_arr[0];
					l_r_arr.shift();
				}
				//
				if(!LorR_grid || (LorR_grid.isOccupied && LorR_grid.isAvaliable) || LorR_grid.isAvaliable)
					return;
				
				oblique_grid = GridDataManager.getGrid(LorR_grid.column,LorR_grid.row+1);
				if(oblique_grid && oblique_grid.isCanFind)
				{
					_path.push(new Point(oblique_grid.column,oblique_grid.row));
					_readlFind(oblique_grid.column,oblique_grid.row);
				}
				else
				{
					if(l_r_arr.length > 0)
					{
						LorR_grid = l_r_arr[0];
						if(!LorR_grid || LorR_grid.isOccupied || LorR_grid.isAvaliable)
							return;
						
						oblique_grid = GridDataManager.getGrid(LorR_grid.column,LorR_grid.row+1);
						if(oblique_grid && oblique_grid.isCanFind)
						{
							_path.push(new Point(oblique_grid.column,oblique_grid.row));
							_readlFind(oblique_grid.column,oblique_grid.row);
						}
					}
				}
			}
		}
		
		public function dispose():void
		{
			_path = null;
		}
	}
}